package com.study.business.interview.service;

import com.alibaba.fastjson.JSONObject;
import com.study.business.interview.domain.Interview;
import com.study.business.interview.mapper.InterviewMapper;
import com.study.business.util.ResponseFailed;
import com.study.business.util.ResponseSuccess;
import com.study.business.util.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author zhang
 */
@Slf4j
@Service
public class InterviewService {
    @Autowired
    private InterviewMapper interviewMapper;

    public ResponseUtil addInterview(Interview interview) {
        interview.setCreateTime(new Date());
        log.info("添加的面试记录是:{}", JSONObject.toJSONString(interview));
        int interviewRtr = interviewMapper.add(interview);
        if (interviewRtr > 0) {
            return new ResponseSuccess(interview.getId());
        }
        return new ResponseFailed();
    }

    public ResponseUtil getInterviews(int pageSize, int currentPage) {
        List<Interview> interviewRtrs = interviewMapper.getAll(pageSize, (currentPage - 1) * pageSize);
        log.info("查询到的面试记录是:{}", JSONObject.toJSONString(interviewRtrs));
        if (null == interviewRtrs) {
            return new ResponseFailed();
        }
        return new ResponseSuccess(interviewRtrs);
    }

    public ResponseUtil deleteInterviews(Integer id) {
        boolean result = interviewMapper.deleteById(id);
        if (!result) {
            return new ResponseFailed();
        }
        return new ResponseSuccess(result);
    }
}
